package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SaleListGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for(int i = 4;i > intCode.toString().length();i--){

            unitCode = "0"+unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }

    @Override
    public Map<String, Object> pageGoods(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        HashMap<String, Object> result = new HashMap<>();
        List<Goods> list = goodsDao.pageGoods(((page-1) * rows),rows,codeOrName,goodsTypeId);
        result.put("rows",list);
        result.put("total",list.size());
        return result;
    }

    @Override
    public void saveOrUpdateGoods(Goods goods) {
        if (goods.getGoodsId() == null){
            //新增
            goods.setInventoryQuantity(0);
            goods.setState(0);
            goodsDao.save(goods);
        } else {
            goodsDao.update(goods);
        }
    }

    @Override
    public void delete(Integer goodsId) {
        Goods goods = goodsDao.getGoodsById(goodsId);
        if (goods.getState() != 0){
            throw new  RuntimeException(ErrorCode.HAS_FORM_ERROR_MESS);
        } else {

            goodsDao.delete(goodsId);
        }

    }

    @Override
    public Map<String, Object> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        HashMap<String, Object> result = new HashMap<>();
        List<Goods> list = goodsDao.getNoInventoryQuantity((page - 1) * rows,rows,nameOrCode);
        result.put("rows",list);
        result.put("total",list.size());
        return result;
    }

    @Override
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        HashMap<String, Object> result = new HashMap<>();
        List<Goods> list = goodsDao.getHasInventoryQuantity((page - 1) * rows,rows,nameOrCode);
        result.put("rows",list);
        result.put("total",list.size());

        return result;
    }

    @Override
    public void saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        goodsDao.saveStock(goodsId,inventoryQuantity,purchasingPrice);
    }

    @Override
    public Map<String, Object> listAlarm() {
        HashMap<String, Object> result = new HashMap<>();
       List<Goods> list =  goodsDao.listAlarm();
        result.put("rows",list);
        return result;
    }


}
